
package approve.action;
import java.net.URLEncoder;
import java.sql.ResultSet;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import memo.action.Memo_listview;
import model.pojo.Employee;
import models.util.MySqlDataAccessHelper;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.LayoutInflater.Factory;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;
import approve.adapter.AdapterListApprover11;
import approve.adapter.ApproverMeetingPendingAdapter;
import approve.adapter.ListEmployeeAdapter;
import approve.dao.ApproveDocDao;
import approve.dao.ListApproveDAO;
import approve.dao.NotApproveDAO;
import approve.model.Approvedoc;
import approve.model.DepartmentLang;
import approve.model.ListApprove;
import approve.model.ListLabel;

public class App_ViewForm11 extends Activity {

	TextView logout,username,welcome,titlehome;
	ImageView imageDelete11,imageSave11, imageSend11;
	Button buttonBack,lbDelete11,lbSend11;
	int totalHeight_lstapp = 0;
	ViewGroup.LayoutParams params_lstapp;
	TextView lbApp,lbWriter,lbDept,lbMeetingDate,lbTitle,lbContent;
	TextView valWrite,valDept,valMeetingDate,valTitle;
	ListView lstApprover;
	public ListView getLstApprover() {
		return lstApprover;
	}
	public void setLstApprover(ListView lstApprover) {
		this.lstApprover = lstApprover;
	}

	WebView valContent;

	Approvedoc approvedoc = new Approvedoc();
	String parentview,functionview;

	ArrayList<DepartmentLang> departlist= new ArrayList<DepartmentLang>();
	DepartmentLang departmentLang = new DepartmentLang();

	List<ListApprove> approverdata;
	ListEmployeeAdapter emadapter,lstapprover;
	ArrayList<Employee> emlist=new ArrayList<Employee>();
	ArrayList<Employee> listemployee_11=new ArrayList<Employee>();

	Bundle extras;

	//waiting
	private final Handler uiHandler=new Handler();
	private boolean isUpdateRequired=false;
	TextView tvwaiting;
	LayoutInflater inflaterwaiting;
	View textEntryViewwaiting;
	AlertDialog.Builder builderwaiting;
	AlertDialog alertwaiting;
	
	ListLabel label;

	public ArrayList<Employee> getListemployee_11() {
		return listemployee_11;
	}
	public void setListemployee_11(ArrayList<Employee> listemployee_11) {
		this.listemployee_11 = listemployee_11;
	}
	public AdapterListApprover11 getApprover11() {
		return approver11;
	}
	public void setApprover11(AdapterListApprover11 approver11) {
		this.approver11 = approver11;
	}
	public boolean isUpdateRequired() {
		return isUpdateRequired;
	}

	public void setUpdateRequired(boolean isUpdateRequired) {
		this.isUpdateRequired = isUpdateRequired;
	}

	public AlertDialog getAlertwaiting() {
		return alertwaiting;
	}

	public void setAlertwaiting(AlertDialog alertwaiting) {
		this.alertwaiting = alertwaiting;
	}

	public Handler getUiHandler() {
		return uiHandler;
	}

	//add approver
	AdapterListApprover11 approver11;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		try{
			initall();
		}catch(Exception e){
			e.printStackTrace();
			Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage( getBaseContext().getPackageName() );
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
		}
	}
	
	private void initall(){
		setContentView(R.layout.app_viewform11);

		loadExtra();

		initalldata();
		getInitAttribute();
		loadData();

		ApproverMeetingPendingAdapter adapter = new ApproverMeetingPendingAdapter(getApplicationContext(), 
				approverdata, LoginActivity.lang);
		lstApprover.setAdapter(adapter);

		totalHeight_lstapp=0;
		for (int i = 0; i < adapter.getCount(); i++) {
			View listItem = adapter.getView(i, null, lstApprover);
			listItem.measure(0, 0);
			totalHeight_lstapp += listItem.getMeasuredHeight();
		}
		
		totalHeight_lstapp+= totalHeight_lstapp + (lstApprover.getDividerHeight() * (adapter.getCount() - 1));
		params_lstapp = lstApprover.getLayoutParams();
		if(totalHeight_lstapp<=0){
			params_lstapp.height =0;
			lstApprover.setLayoutParams(params_lstapp);
		}else{
			if(totalHeight_lstapp<200){
				params_lstapp.height=android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
				lstApprover.setLayoutParams(params_lstapp);
			}else{
				params_lstapp.height=200;
				lstApprover.setLayoutParams(params_lstapp);
			}
			
		}
		
		lstApprover.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View arg0, MotionEvent event) {
				if(event.getAction() == MotionEvent.ACTION_MOVE){
					lstApprover.scrollBy(0, 1);
				}

				return false;
			}
		});


		logout.setOnClickListener(listenerLogout);

		buttonBack.setOnClickListener(processBack);

		RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relativeTitle);

		Resources res = getResources();
		final BitmapDrawable background = (BitmapDrawable) res.getDrawable(R.drawable.bg_menu_item);
		background.setTileModeX(Shader.TileMode.REPEAT);
		background.setTileModeY(Shader.TileMode.REPEAT);
		background.setDither(true);
		relativeLayout.setBackgroundDrawable(background);
	}
	
	public void loadExtra() {
		extras = getIntent().getExtras();
		if (extras != null) {
			parentview=(String)extras.get("parentview");
			functionview=(String)extras.getString("functionview");
			approvedoc=(Approvedoc) extras.get("approvedoc");
			label = (ListLabel)extras.get("label");
		}
	}

	public void getInitAttribute(){
		int lbwidth=LoginActivity.screenwidth/3+10;
		int tvwidth=LoginActivity.screenwidth-LoginActivity.screenwidth/3;

		logout=(TextView)findViewById(R.id.logout);
		logout.setText(LoginActivity.lang.getString("Logout"));

		username=(TextView)findViewById(R.id.username);
		username.setText(LoginActivity.emp.getUserName());

		welcome = (TextView)findViewById(R.id.welcome);
		welcome.setText(LoginActivity.lang.getString("ac_Welcome"));

		buttonBack = (Button)findViewById(R.id.buttonBack);
		titlehome=(TextView)findViewById(R.id.titlehome);
		if(parentview.equals("notApprove")){
			titlehome.setText(LoginActivity.lang.getString("not-approved-document"));
		}else if(parentview.equals("approveDoc")){
			titlehome.setText(LoginActivity.lang.getString("document_approval"));
		}else if(parentview.equals("declineDoc")){
			titlehome.setText(LoginActivity.lang.getString("docDecline"));
		}else if(parentview.equals("referApprove")){
			titlehome.setText(LoginActivity.lang.getString("reference_document")+"-"+LoginActivity.lang.getString("empApprove"));
		}else if(parentview.equals("referWait")){
			titlehome.setText(LoginActivity.lang.getString("reference_document")+"-"+LoginActivity.lang.getString("empNotApprove"));
		}else if(parentview.equals("docPending")){
			titlehome.setText(LoginActivity.lang.getString("pending-document"));
		}else if(parentview.equals("library")){
			titlehome.setText(LoginActivity.lang.getString("library_document"));
		}else{
			titlehome.setText(LoginActivity.lang.getString("metting_pending"));
		}

		lbApp = (TextView)findViewById(R.id.lbApprover11);
		lbApp.setText(LoginActivity.lang.getString("approver") +": ");
		lbApp.setTextColor(Color.YELLOW);
		lbWriter = (TextView)findViewById(R.id.lbEmp11);
		lbWriter.setText(LoginActivity.lang.getString("writer")+":");
		lbWriter.setTextColor(Color.YELLOW);
		lbDept = (TextView)findViewById(R.id.lbDept11);
		lbDept.setText(LoginActivity.lang.getString("ac_MNLDepartmentCode")+":");
		lbDept.setTextColor(Color.YELLOW);
		lbMeetingDate = (TextView)findViewById(R.id.lbMeetingDate11);
		lbMeetingDate.setText(LoginActivity.lang.getString("meeting_date")+":");
		lbMeetingDate.setTextColor(Color.YELLOW);
		lbTitle = (TextView)findViewById(R.id.lbTitle11);
		lbTitle.setText(LoginActivity.lang.getString("title")+":  ");
		lbTitle.setTextColor(Color.YELLOW);
		lbContent = (TextView)findViewById(R.id.lbContent11);
		lbContent.setText(LoginActivity.lang.getString("content")+":  ");
		lbContent.setTextColor(Color.YELLOW);

		valWrite = (TextView)findViewById(R.id.valueEmp11);
		valDept = (TextView)findViewById(R.id.valueDept11);
		valMeetingDate = (TextView)findViewById(R.id.valueMeetingDate11);
		valTitle = (TextView)findViewById(R.id.valueTitle11);
		valContent = (WebView)findViewById(R.id.valueContent11);

		lbWriter.setWidth(lbwidth);
		valWrite.setWidth(tvwidth);

		lbDept.setWidth(lbwidth);
		valDept.setWidth(tvwidth);

		lbMeetingDate.setWidth(lbwidth);
		valMeetingDate.setWidth(tvwidth);

		lbTitle.setWidth(lbwidth);
		valTitle.setWidth(tvwidth);

		lstApprover = (ListView)findViewById(R.id.listApprover11);

		if(functionview.equals("view")){
			imageSend11 = (ImageView)findViewById(R.id.imageSend11);
			lbSend11 = (Button)findViewById(R.id.lbSend11);
			imageDelete11 = (ImageView)findViewById(R.id.imageDelete11);
			lbDelete11 = (Button)findViewById(R.id.lbDelete11);

			if(parentview.equals("my_meetingPending")){

				lbDelete11.setText(LoginActivity.lang.getString("Delete"));

				imageDelete11.setVisibility(View.VISIBLE);
				lbDelete11.setVisibility(View.VISIBLE);

				imageSend11.setVisibility(View.GONE);
				lbSend11.setVisibility(View.GONE);

				imageDelete11.setOnClickListener(processDelete);
				lbDelete11.setOnClickListener(processDelete);
			}else if(parentview.equals("my_saveDoc")){

				lbSend11.setText(LoginActivity.lang.getString("sendapprover"));
				lbDelete11.setText(LoginActivity.lang.getString("Delete"));

				imageSend11.setVisibility(View.VISIBLE);
				lbSend11.setVisibility(View.VISIBLE);

				imageDelete11.setVisibility(View.VISIBLE);
				lbDelete11.setVisibility(View.VISIBLE);

				imageDelete11.setOnClickListener(processDelete);
				lbDelete11.setOnClickListener(processDelete);

				lbSend11.setOnClickListener(processApproval);
				imageSend11.setOnClickListener(processApproval);
			}else if(parentview.equals("meetingPending")){

				lbSend11.setText(LoginActivity.lang.getString("sendapprover"));
				lbDelete11.setText(LoginActivity.lang.getString("Delete"));

				imageSend11.setVisibility(View.GONE);
				lbSend11.setVisibility(View.GONE);

				imageDelete11.setVisibility(View.GONE);
				lbDelete11.setVisibility(View.GONE);
			}
		}else if(functionview.equals("view_label")){
			imageSend11 = (ImageView)findViewById(R.id.imageSend11);
			lbSend11 = (Button)findViewById(R.id.lbSend11);
			imageDelete11 = (ImageView)findViewById(R.id.imageDelete11);
			lbDelete11 = (Button)findViewById(R.id.lbDelete11);
			
			imageSend11.setVisibility(View.GONE);
			lbSend11.setVisibility(View.GONE);

			imageDelete11.setVisibility(View.GONE);
			lbDelete11.setVisibility(View.GONE);
		}

		inflaterwaiting = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);          
		textEntryViewwaiting = inflaterwaiting.inflate(R.layout.waiting, null);
		tvwaiting=(TextView)textEntryViewwaiting.findViewById(R.id.TextView01);					
		tvwaiting.setText(LoginActivity.lang.getString("waiting")+"...");

		builderwaiting = new AlertDialog.Builder(App_ViewForm11.this);
		builderwaiting.setView(textEntryViewwaiting);
		builderwaiting.setCancelable(false);
		alertwaiting = builderwaiting.create();
	}

	private void initalldata(){
		approverdata = new ArrayList<ListApprove>();
		MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();
		try{
			mysql.open(LoginActivity.database);
			String sql="SELECT deplang.DepartmentName,emp.FullName FROM department_lang deplang,employee emp  WHERE deplang.IDDepartment = emp.IDDepartment and emp.IDEmployee = '"+approvedoc.getiDEmployee()+"'  and deplang.LangID='"+LoginActivity.typeLang+"'";
			ResultSet rs = mysql.executeQuery(sql);
			if(rs.next()){
				approvedoc.setFullName(rs.getString("FullName"));
				approvedoc.setDeparment(rs.getString("DepartmentName"));
			}else{
				approvedoc.setFullName("");
				approvedoc.setDeparment("");
			}

			sql="select em.FullName, de.DepartmentName from listapprove la, approvedoc ad , employee em, department_lang de where la.IDGroupApprove=ad.IDGroupApprove and em.IDEmployee=la.IDEmployee and em.IDDepartment=de.IDDepartment and ad.IDApproveDoc='"+approvedoc.getiDApproveDoc()+"' and de.LangID='"+LoginActivity.typeLang+"'";
			rs = mysql.executeQuery(sql);
			while(rs.next()){
				approverdata.add(new ListApprove(rs.getString("FullName"),rs.getString("DepartmentName"),"null1"));
			}
			rs.close();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			mysql.close();
		}
	}

	private void loadData(){
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		valWrite.setText(approvedoc.getFullName());
		valDept.setText(approvedoc.getDeparment());

		valMeetingDate.setText(df.format(approvedoc.getTimecreate()));
		valTitle.setText(approvedoc.getTitle());
		
		valContent.getSettings().setLoadWithOverviewMode(true);
		valContent.getSettings().setUseWideViewPort(true);
		valContent.getSettings().setJavaScriptEnabled(true);
		valContent.setBackgroundColor(Color.parseColor("#364b4c"));
		valContent.setClickable(false);
//		valContent.getSettings().getBuiltInZoomControls();
		valContent.setHorizontalScrollBarEnabled(true);
		valContent.setScrollbarFadingEnabled(true);
		valContent.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
		valContent.getSettings().setSupportZoom(false);
		valContent.getSettings().setBuiltInZoomControls(false);
//		valContent.setClickable(false);

		final String mimeType = "text/html";
		String header = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
		header += "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML Basic 1.1//EN\" \"http://www.w3.org/TR/xhtml-basic/xhtml-basic11.dtd\">";
		header += "<html><head><meta name=\"viewport\" content=\"width=device-width,height=device-height, initial-scale=1.0, user-scalable=yes\"></head>" +
		"<body style=\"font-family:Arial, Helvetica, sans-serif; font-size:15px;!important\">";
		String str=header + "<font color='white'>"+ approvedoc.getContent()+"</font>" +"</body></html>";
		
		valContent.loadData(URLEncoder.encode(str).replaceAll("\\+"," "), mimeType,null);

		LinearLayout linerform11 = (LinearLayout)findViewById(R.id.linerform11);
		linerform11.setBackgroundColor(Color.parseColor("#364b4c"));

	}

	public List<ListApprove> getAllListApproveByGroupID(){
		return ListApproveDAO.getApproverMeeting11(approvedoc.getiDApproveDoc(),LoginActivity.database,LoginActivity.typeLang);
	}

	private OnClickListener listenerLogout=new OnClickListener() {

		public void onClick(View v) {
			logout.setTextColor(Color.parseColor("#1188ff"));
			LoginActivity.flagruntimer=true;
			LoginActivity.soluong=0;
			LoginActivity.emp=new Employee();
			MainEApproval.mNotificationManager.cancel(MainEApproval.SIMPLE_NOTFICATION_ID);
			MainEApproval.timer.cancel();
			Intent i=new Intent(App_ViewForm11.this, LogoutActivity.class);
			startActivity(i);
			finish();
		}
	};
	@Override
	public void onBackPressed() {

		if(parentview.equals("notApprove")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_NotApproveDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("approveDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_ApprovedDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("declineDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_DeclineDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("referApprove")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_ReferenceDoc_Approved.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("referWait")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_ReferenceDoc_Waiting.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("docPending")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_PendingDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("my_approveDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,My_App_ApproveDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("my_declineDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,My_App_DeclineDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("my_meetingPending")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,My_App_MeetingSendingDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("my_saveDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,My_App_SaveDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("my_sendingDoc")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,My_App_SendingDocument.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else if(parentview.equals("library")){
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,Library_Label_Item.class);
						intent.putExtra("label", label);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}else{
			
			App_ViewForm11.this.getAlertwaiting().show();
			try{
				new Thread(){
					public void run() {
						Intent intent = new Intent(App_ViewForm11.this,App_MeetingDocPending.class);
						startActivity(intent);
						App_ViewForm11.this.getUiHandler().post( new Runnable(){
							@Override
							public void run() {
								if(App_ViewForm11.this.isUpdateRequired()){
								}else{
									App_ViewForm11.this.getAlertwaiting().cancel();
								}
							}
						} );
					}
				}.start();
			}catch (Exception e) {}
		}
		finish();
	}

	public OnClickListener processDelete = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			AlertDialog.Builder builder = new AlertDialog.Builder(App_ViewForm11.this);
			builder.setTitle(LoginActivity.lang.getString("Delete"));
			builder.setMessage(LoginActivity.lang.getString("areyousure"));


			builder.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					
					alertwaiting.show();

					try{
						new Thread(){
							public void run() {
								ApproveDocDao.deleteMydocumentSending(approvedoc.getiDApproveDoc(), LoginActivity.database);

								uiHandler.post( new Runnable(){
									@Override
									public void run() {
										if(isUpdateRequired){
										}else{
											if(parentview.equals("my_meetingPending")){
												Intent intent = new Intent(App_ViewForm11.this,My_App_MeetingSendingDocument.class);
												startActivity(intent);
												finish();
											}else if(parentview.equals("my_saveDoc")){
												Intent intent = new Intent(App_ViewForm11.this,My_App_SaveDocument.class);
												startActivity(intent);
												finish();
											}
											alertwaiting.cancel();
										}
									}
								} );
							}
						}.start();
					}catch (Exception e) {}
				}
			});
			builder.setNegativeButton(LoginActivity.lang.getString("cancel"),new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
				}
			});

			AlertDialog alertDialog = builder.create();
			alertDialog.show();

			Button buttonok = alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
			if(buttonok!=null){
				buttonok.setBackgroundResource(R.drawable.bt_general_focused);
				buttonok.setGravity(Gravity.CENTER);
				buttonok.setTextColor(Color.WHITE);
			}
			Button buttoncancel = alertDialog.getButton(DialogInterface.BUTTON_NEGATIVE);
			if(buttoncancel!=null){
				buttoncancel.setBackgroundResource(R.drawable.bt_general_focused);
				buttonok.setGravity(Gravity.CENTER);
				buttoncancel.setTextColor(Color.WHITE);
			}
		}
	};

	public OnClickListener processApproval = new OnClickListener() {

		@Override
		public void onClick(View arg0) {

			LayoutInflater inflater = (LayoutInflater) App_ViewForm11.this.getSystemService(LAYOUT_INFLATER_SERVICE);  
			final View view = inflater.inflate(R.layout.adapter_add_approver,null);

			final TextView lbdept = (TextView)view.findViewById(R.id.lbDept11);
			lbdept.setText(LoginActivity.lang.getString("ac_MNLDepartmentCode"));
			lbdept.setTextColor(Color.YELLOW);

			final Spinner spinnerDept = (Spinner)view.findViewById(R.id.spinnerDept11);
			final TextView lbLstApp = (TextView)view.findViewById(R.id.lbListAppver11);
			lbLstApp.setText(LoginActivity.lang.getString("ListApprove"));
			lbLstApp.setTextColor(Color.YELLOW);

			final ListView lstappver = (ListView)view.findViewById(R.id.listAprver11);
			LayoutParams pr=(LayoutParams)lstappver.getLayoutParams();
			pr.height=LoginActivity.screenheight;
			lstappver.setLayoutParams(pr);
			approver11 = new AdapterListApprover11(App_ViewForm11.this, listemployee_11);
			lstappver.setAdapter(approver11);



			departlist=NotApproveDAO.getListDepartment1(LoginActivity.lang.getString("choosedepartment"),LoginActivity.typeLang, LoginActivity.database);
			final ArrayAdapter<DepartmentLang> departadapter=new ArrayAdapter<DepartmentLang>(App_ViewForm11.this,android.R.layout.simple_spinner_dropdown_item,departlist);
			departadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

			spinnerDept.setAdapter(departadapter);
			spinnerDept.setOnItemSelectedListener(new OnItemSelectedListener() {

				@Override
				public void onItemSelected(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					if(arg2!=0){
						departmentLang = (DepartmentLang)arg0.getItemAtPosition(arg2);

						LayoutInflater inflaterMem = (LayoutInflater)getWindow().getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
						final View viewMem = inflaterMem.inflate(R.layout.adapter_list_member, null);

						final ListView lstMember11 = (ListView)viewMem.findViewById(R.id.listMember11);
						LayoutParams pr1=(LayoutParams)lstMember11.getLayoutParams();
						pr1.height=LoginActivity.screenheight;
						pr1.width=LoginActivity.screenwidth;
						lstMember11.setLayoutParams(pr1);
						final ListEmployeeAdapter employeeAdapter = new ListEmployeeAdapter(getWindow().getContext(),NotApproveDAO.getListEmployeeSubRefer(departmentLang.getId(), LoginActivity.database,approver11.getStrid()));
						lstMember11.setAdapter(employeeAdapter);

						AlertDialog.Builder builder = new AlertDialog.Builder(getWindow().getContext());
						builder.setTitle(LoginActivity.lang.getString("ac_ma_ListEmployee"));
						builder.setCancelable(false);
						builder.setView(viewMem);

						builder.setPositiveButton(LoginActivity.lang.getString("add11"), new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								if(employeeAdapter.getListCheck()!=null && employeeAdapter.getListCheck().size() > 0){
									for (int i = 0; i < employeeAdapter.getListCheck().size(); i++) {
										Employee employee = employeeAdapter.getListCheck().get(i);
										listemployee_11.add(employee);
									}
									approver11 = new AdapterListApprover11(App_ViewForm11.this, listemployee_11);
									lstappver.setAdapter(approver11);
								}
								spinnerDept.setAdapter(departadapter);
							}
						});

						builder.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface arg0, int arg1) {
								spinnerDept.setAdapter(departadapter);
							}
						});

						AlertDialog alertDialog = builder.create();
						alertDialog.show();

					}
				}

				@Override
				public void onNothingSelected(AdapterView<?> arg0) {
				}
			});


			AlertDialog.Builder builder = new AlertDialog.Builder(App_ViewForm11.this);
			builder.setTitle(LoginActivity.lang.getString("add_approver"));
			builder.setCancelable(false);
			builder.setView(view);


			builder.setPositiveButton(LoginActivity.lang.getString("ok"), new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
					if(listemployee_11.size() > 0){

						alertwaiting.show();

						try{
							new Thread(){
								public void run() {

									String id = new Date().getTime()+"";
									String sql="INSERT INTO listapprove(IDGroupApprove,IDEmployee,IDtypeApprove,listapprove.Order,IDStatus) ";
									String strapp="";
									for(int i=0;i<listemployee_11.size();i++){
										if(strapp.equals("")){
											strapp=" VALUES('"+id+"','"+listemployee_11.get(i).getIdemployee()+"',2,1,2)";
										}else{
											strapp+=",('"+id+"','"+listemployee_11.get(i).getIdemployee()+"',2,1,2)";
										}
									}
									if(!strapp.equals("")){
										sql+=strapp;
										MySqlDataAccessHelper mysql = new MySqlDataAccessHelper();
										try{
											mysql.open(LoginActivity.database);
											if(mysql.executeUpdate(sql)>0){
												sql="UPDATE approvedoc SET IDGroupApprove='"+id+"',IDStatus=2,Timecreate=NOW() WHERE IDApproveDoc='"+approvedoc.getiDApproveDoc()+"'";
												mysql.executeUpdate(sql);
											}

										}catch(Exception e){
											e.printStackTrace();
										}finally{
											mysql.close();
										}
									}

									uiHandler.post( new Runnable(){
										@Override
										public void run() {
											if(isUpdateRequired){
											}else{
												Intent intent = new Intent(App_ViewForm11.this,My_App_MeetingSendingDocument.class);
												startActivity(intent);
												finish();
												alertwaiting.cancel();
											}
										}
									} );
								}
							}.start();
						}catch (Exception e) {}
					}

				}
			});

			builder.setNegativeButton(LoginActivity.lang.getString("cancel"), new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface arg0, int arg1) {
				}
			});
			AlertDialog alertDialog = builder.create();
			alertDialog.show();

		}
	};

	// Main menu
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = new MenuInflater(getApplicationContext());
		if(LoginActivity.typeLang.equals("EN")){
			inflater.inflate(R.menu.option_main_menu, menu);
		}else if(LoginActivity.typeLang.equals("VN")){
			inflater.inflate(R.menu.option_main_menuvn, menu);
		}else{
			inflater.inflate(R.menu.option_main_menukr, menu);
		}

		setMenuBackground();
		return true;
	}
	protected void setMenuBackground(){
		Log.d("", "Enterting setMenuBackGround");  
		getLayoutInflater().setFactory( new Factory() {  


			@Override
			public View onCreateView(String name, Context context,
					AttributeSet attrs) {
				if ( name.equalsIgnoreCase( "com.android.internal.view.menu.IconMenuItemView" ) ) {  

					try { 
						LayoutInflater f = getLayoutInflater();  
						final View view = f.createView( name, null, attrs );  
						new Handler().post( new Runnable() {  
							public void run () {  
								view.setBackgroundResource( R.drawable.border_footer);  
							}  
						} );  
						return view;  
					}  
					catch ( InflateException e ) {}  
					catch ( ClassNotFoundException e ) {}  
				}  
				return null;  

			}  
		});  
	}
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if(LoginActivity.typeLang.equals("VN")){
			switch (item.getItemId()) {
			case R.id.menu_eappvn:
				processMenuEApproval();
				return true;
			case R.id.menu_primemovn:
				processMenuPrivateMemo();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}else if(LoginActivity.typeLang.equals("KR")){
			switch (item.getItemId()) {
			case R.id.menu_eappkr:
				processMenuEApproval();
				return true;
			case R.id.menu_primemokr:
				processMenuPrivateMemo();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}else{
			switch (item.getItemId()) {
			case R.id.menu_eapp:
				processMenuEApproval();
				return true;
			case R.id.menu_primemo:
				processMenuPrivateMemo();
				return true;
			default:
				return super.onOptionsItemSelected(item);
			}
		}
	}

	// process on click menu e-approval
	public void processMenuEApproval(){
		Intent intent = new Intent(App_ViewForm11.this,MainEApproval.class);
		startActivity(intent);
		finish();
	}

	public void processMenuPrivateMemo(){
		Intent intent = new Intent(App_ViewForm11.this,Memo_listview.class);
		startActivity(intent);
		finish();
	}

	public OnClickListener processBack = new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			buttonBack.setBackgroundResource(R.drawable.bg_back_pressed);
			if(parentview.equals("notApprove")){
				Intent intent = new Intent(App_ViewForm11.this,App_NotApproveDocument.class);
				startActivity(intent);
			}else if(parentview.equals("approveDoc")){
				Intent intent = new Intent(App_ViewForm11.this,App_ApprovedDocument.class);
				startActivity(intent);
			}else if(parentview.equals("declineDoc")){
				Intent intent = new Intent(App_ViewForm11.this,App_DeclineDocument.class);
				startActivity(intent);
			}else if(parentview.equals("referApprove")){
				Intent intent = new Intent(App_ViewForm11.this,App_ReferenceDoc_Approved.class);
				startActivity(intent);
			}else if(parentview.equals("referWait")){
				Intent intent = new Intent(App_ViewForm11.this,App_ReferenceDoc_Waiting.class);
				startActivity(intent);
			}else if(parentview.equals("docPending")){
				Intent intent = new Intent(App_ViewForm11.this,App_PendingDocument.class);
				startActivity(intent);
			}else if(parentview.equals("my_approveDoc")){
				Intent intent = new Intent(App_ViewForm11.this,My_App_ApproveDocument.class);
				startActivity(intent);
			}else if(parentview.equals("my_declineDoc")){
				Intent intent = new Intent(App_ViewForm11.this,My_App_DeclineDocument.class);
				startActivity(intent);
			}else if(parentview.equals("my_meetingPending")){
				Intent intent = new Intent(App_ViewForm11.this,My_App_MeetingSendingDocument.class);
				startActivity(intent);
			}else if(parentview.equals("my_saveDoc")){
				Intent intent = new Intent(App_ViewForm11.this,My_App_SaveDocument.class);
				startActivity(intent);
			}else if(parentview.equals("my_sendingDoc")){
				Intent intent = new Intent(App_ViewForm11.this,My_App_SendingDocument.class);
				startActivity(intent);
			}else if(parentview.equals("library")){
				Intent intent = new Intent(App_ViewForm11.this,Library_Label_Item.class);
				intent.putExtra("label", label);
				startActivity(intent);
			}else{
				Intent intent = new Intent(App_ViewForm11.this,App_MeetingDocPending.class);
				startActivity(intent);
			}
			finish();
		}
	};
}